#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "euler022.h"

int cmp (const void* a, const void *b) {
//	printf ("%s %s %d\n", (char**)a, (char**)b, strcmp ((char const*)a, (char const*)b));
	return strcmp (*(char const**)a, *(char const**)b);
}

char* A[] = {"AAA","DDD","CCC","BBB"};

int calc (char * name) {
	int s = 0;
	char *c = name;
	while (*c != 0) {
		//printf ("%d ", *c - 'A' + 1);
		s += *c - 'A' + 1;
		c++;
	}
	//printf ("\n");
	return s;
}

int main() {
	int i;
	
	//qsort (A, sizeof(A) / sizeof(A[0]), sizeof(A[0]), cmp);
	qsort (NAMES, sizeof(NAMES) / sizeof(NAMES[0]), sizeof(char*), cmp);
	int s = 0;
	for (i = 0; i < sizeof(NAMES) / sizeof(NAMES[0]); i++) {
		//printf ("%d sum(%s)=%d\n", i+1, NAMES[i], calc(NAMES[i]));
		//printf ("%s\n", NAMES[i]);
		s += (i+1) * calc(NAMES[i]);
	}
	printf ("%d\n", s);
	
	return 0;
}
